package com.fdb.smcts.core.validate;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.fdb.smcts.core.annotation.NumType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 数字校验类
  * TODO
  * @author heyingfeng
  * @since 2019年1月21日 上午10:59:58
  * @version 1.0
 */
public class NumTypeValidatorImpl implements ConstraintValidator<NumType, String>{

	private static Logger logger = LoggerFactory.getLogger(NumTypeValidatorImpl.class);
	
	@Override
	public void initialize(NumType constraintAnnotation) {
		
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		try {
			if (value == null || value.equals("")) {
				return true;
			}
			Pattern pattern = Pattern.compile("^[-]{0,1}(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,4})?$") ;
			Matcher match = pattern.matcher(value);
			if(match.matches()==true) {
				return true;
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return false;
	}

}
